package cn.gzsxt.music.mapper;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import cn.gzsxt.music.query.SongListQueryObject;

public class SongListProvider {
	public String getAllSongList(SongListQueryObject qo) {
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("select * from mc_songlist");
		if (StringUtils.isNotBlank(qo.getKeyword())) {
			sBuilder.append(" where songList_name like '%' #{keyword} '%'");
		}
		sBuilder.append(" limit #{beginIndex},#{rows}");
		System.out.println(sBuilder.toString());
		return sBuilder.toString();
	}

	public String getAllSongListTotal(SongListQueryObject qo) {
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("select count(*) from mc_songlist");
		if (StringUtils.isNotBlank(qo.getKeyword())) {
			sBuilder.append(" where songList_name like '%' #{keyword} '%'");
		}
		System.out.println(sBuilder.toString());
		return sBuilder.toString();
	}

	public String getLabelBysongList_id(String label) {
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("select * from mc_label where label_id in (");
		String[] labels = label.split(",");
		for (int i = 0; i < labels.length; i++) {
			sBuilder.append(labels[i] + ",");
		}
		sBuilder.deleteCharAt(sBuilder.length() - 1);
		sBuilder.append(")");
		return sBuilder.toString();
	}

	public String selectLabelName(String label) {
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("select group_concat(l.label_name) songList_label from mc_label l where l.label_id in (");
		String[] labels = label.split(",");
		for (int i = 0; i < labels.length; i++) {
			sBuilder.append(labels[i] + ",");
		}
		sBuilder.deleteCharAt(sBuilder.length() - 1);
		sBuilder.append(")");
		return sBuilder.toString();
	}

	public String saveSongList(Map<String, Object> params) {
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("insert into mc_songlist ( ");

		if (StringUtils.isNotBlank((String) params.get("songList_name"))) {
			sBuilder.append("songList_name,");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_hit"))) {
			sBuilder.append("songList_hit,");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_info"))) {
			sBuilder.append("songList_info,");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_label"))) {
			sBuilder.append("songList_label,");
		}
		if (StringUtils.isNotBlank((String) params.get("user_id"))) {
			sBuilder.append("user_id,");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_like"))) {
			sBuilder.append("songList_like,");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_status"))) {
			sBuilder.append("songList_status,");
		}
		sBuilder.deleteCharAt(sBuilder.length() - 1);
		sBuilder.append(") values (");

		if (StringUtils.isNotBlank((String) params.get("songList_name"))) {
			sBuilder.append("#{songList_name},");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_hit"))) {
			sBuilder.append("#{songList_hit},");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_info"))) {
			sBuilder.append("#{songList_info},");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_label"))) {
			sBuilder.append("#{songList_label},");
		}
		if (StringUtils.isNotBlank((String) params.get("user_id"))) {
			sBuilder.append("#{user_id},");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_like"))) {
			sBuilder.append("#{songList_like},");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_status"))) {
			sBuilder.append("#{songList_status},");
		}
		sBuilder.deleteCharAt(sBuilder.length() - 1);
		sBuilder.append(")");

		System.out.println(sBuilder.toString());
		return sBuilder.toString();
	}

	public String updateSongList(Map<String, Object> params) {
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("update mc_songlist set ");

		if (StringUtils.isNotBlank((String) params.get("songList_name"))) {
			sBuilder.append("songList_name = #{songList_name} ,");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_hit"))) {
			sBuilder.append("songList_hit = #{songList_hit} ,");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_info"))) {
			sBuilder.append("songList_info = #{songList_info} ,");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_label"))) {
			sBuilder.append("songList_label = #{songList_label} ,");
		}
		if (StringUtils.isNotBlank((String) params.get("user_id"))) {
			sBuilder.append("user_id = #{user_id} ,");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_like"))) {
			sBuilder.append("songList_like = #{songList_like} ,");
		}
		if (StringUtils.isNotBlank((String) params.get("songList_status"))) {
			sBuilder.append("songList_status = #{songList_status} ,");
		}
		sBuilder.deleteCharAt(sBuilder.length() - 1);
		sBuilder.append("where songList_id = #{songList_id}");

		return sBuilder.toString();
	}

}
